package com.llb666.shop.cart.domain.service;

import com.alibaba.fastjson2.JSON;
import com.llb666.common.core.constant.AppConstants;
import com.llb666.common.redis.service.RedisService;
import com.llb666.shop.cart.domain.entity.ShopCartBo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车service.
 *
 * @author llb666
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ShopCartService {

    private final RedisService redisService;

    /**
     * 新增购物车商品."+"按钮
     *
     * @param userId     登录用户
     * @param shopCartBo 购物车商品
     */
    public void add(String userId, ShopCartBo shopCartBo) {
        // 前端用户在登录的情况下，添加商品到购物车，会同时在后端同步购物车到redis缓存
        // 购物车如果已经存在该商品，数量累加
        String shopCartJson = redisService.getCacheObject(AppConstants.SHOP_MALL_CART + ":" + userId);
        List<ShopCartBo> shopCartList = null;
        if (StringUtils.isNotBlank(shopCartJson)) {
            // redis中已经有购物车了
            shopCartList = JSON.parseArray(shopCartJson, ShopCartBo.class);
            // 判断购物车中是否存在已有商品，如果有的话counts累加
            boolean isHaving = false;
            for (ShopCartBo sc : shopCartList) {
                String tmpSpecId = sc.getSpecId();
                //商品规格Id
                if (tmpSpecId.equals(shopCartBo.getSpecId())) {
                    sc.setBuyCounts(sc.getBuyCounts() + shopCartBo.getBuyCounts());
                    isHaving = true;
                }
            }
            if (!isHaving) {
                shopCartList.add(shopCartBo);
            }
        } else {
            // redis中没有购物车
            shopCartList = new ArrayList<>();
            // 直接添加到购物车中
            shopCartList.add(shopCartBo);
        }
        // 覆盖现有redis中的购物车
        redisService.setCacheObject(AppConstants.SHOP_MALL_CART + ":" + userId, JSON.toJSONString(shopCartList));
    }

    /**
     * 删除购物车商品. "-" 按钮
     * @param userId 登录用户id
     * @param itemSpecId 购物车商品规格
     */
    public void del(String userId, String itemSpecId) {
        // 用户在页面删除购物车中的商品数据，如果此时用户已经登录，则需要同步删除后端redis购物车中的商品
        String shopCartJson = redisService.getCacheObject(AppConstants.SHOP_MALL_CART + ":" + userId);
        if (StringUtils.isNotBlank(shopCartJson)) {
            // redis中已经有购物车了
            List<ShopCartBo> shopCartList = JSON.parseArray(shopCartJson, ShopCartBo.class);
            // 判断购物车中是否存在已有商品，如果有的话则删除
            for (ShopCartBo sc : shopCartList) {
                String tmpSpecId = sc.getSpecId();
                if (tmpSpecId.equals(itemSpecId)) {
                    shopCartList.remove(sc);
                    break;
                }
            }
            // 覆盖现有redis中的购物车
            redisService.setCacheObject(AppConstants.SHOP_MALL_CART + ":" + userId, JSON.toJSONString(shopCartList));
        }
    }
}
